// pages/shop/shop-car.js
const app = getApp();
const util = require('../../utils/util.js');
let _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopCarList: [{ 'txtStyle': 'left:0px;' }, { 'txtStyle': 'left:0px;'}],
    delBtnWidth: 150,//删除按钮宽度单位（rpx）
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this;
    _this.setData({
      userInfo: app.globalData.xieYouUserInfo
    })
    _this.getShopCar();
  },
  init:function(){
    _this.setData({
      userInfo: app.globalData.xieYouUserInfo
    })
    _this.getShopCar();
  },
  // 获取购物车信息
  getShopCar:function(){
    app.getMassage(null, _this.data.userInfo.code,'getShopCar',function(res){
      console.log(res);
      let data = res.data.items, checkedAll = true;
      for (let i = 0; i <data.length; i++){
        if (!data[i].checked){
          checkedAll = false
        }
      }
      _this.setData({
        shopCarList: res.data.items,
        discountedFeeTotal: res.data.discountedFeeTotal,
        checkedAll: checkedAll,
        shopId: res.data.id
      })
      wx.setStorage({
        key: 'shopCar',
        data: res.data,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
    },'GET')
  },
  dataChange: function (e) {
    let shopCarList = _this.data.shopCarList,
      type = e.currentTarget.dataset.type,
      index = e.currentTarget.dataset.index,
      data = shopCarList[index];
    if (type == 'less' && data.product.count == 0) return;
    // type == 'add' ? data.product.count++ : data.product.count--;
    type == 'add' ? data.count++ : data.count--;
    _this.addShopCar(data);
  },
  addShopCar: function (data) {
    app.getMassage(data, _this.data.shopId + '/item/new', 'shopAdd', function (res) {
      if (res.status == 'success') {
        _this.getShopCar();
        console.log('res');
      }
    })
  },
  checkedFun:function(e){
    let checked = e.currentTarget.dataset.operate,
      shopCarList = _this.data.shopCarList,
      data = [];
    let url = _this.data.shopId + '/item/'
    if (checked == 'checked') {
      
      let index = e.currentTarget.dataset.index;
      shopCarList[index].checked = !shopCarList[index].checked;
      for (let i = 0; i < shopCarList.length; i++) {
        shopCarList[i].checked ? data.push(shopCarList[i].id) : "";
      }
      if (data.length == 0){
        data[0] = shopCarList[index].id;
        url = url + false;
      }else{
        url = url + true;
      }
    } else {
      let checkedAll = _this.data.checkedAll;
      url = url + !checkedAll;
      for (let i = 0; i < shopCarList.length; i++) {
        data.push(shopCarList[i].id)
      }
    }
    app.getMassage(data, url, 'shopChecked', function (res) {
      if (res.status == 'success') {
        _this.getShopCar();
      }
    }, 'PUT')
  },
  dataChangeDelete:function(e){
    let index = e.currentTarget.dataset.index,
      shopCarList = _this.data.shopCarList;

    app.getMassage(null, shopCarList[index].id,'shopDelete',function(res){
      if (res.status == 'success') {
        _this.getShopCar();
      }
    },'DELETE')
  },
  payOder:function(e){
    let shopCarList = _this.data.shopCarList;
    let data = true
    for (let i = 0; i < shopCarList.length; i++){
      shopCarList[i].checked?data = false:'';
    }
    if(data){
      util.alertMsg('提示', '未选择商品', function () {
        return false;
      }, null, false);
      return false;
    }
    _this.goNavTo(e);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  init:function(){
    _this.initEleWidth();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 滑动删除
   */
  touchS: function (e) {
    var index = e.currentTarget.dataset.index,
      shopCarList = _this.data.shopCarList;
    for (var i = 0; i < shopCarList.length; i++) {
      if (shopCarList[i].shopCarList) {
        for (var a = 0; a < shopCarList[i].shopCarList.length; a++) {
          shopCarList[i].shopCarList[a].txtStyle = 'left:0px;';
        }
      }
    }
    _this.setData({
      shopCarList: shopCarList
    })
    if (e.touches.length == 1) {
      _this.setData({
        //设置触摸起始点水平方向位置
        startX: e.touches[0].clientX
      });
    }
  },
  touchM: function (e) {
    var test = true;
    if (e.touches.length == 1) {
      //手指移动时水平方向位置
      var moveX = e.touches[0].clientX;
      //手指起始点位置与移动期间的差值
      var disX = _this.data.startX - moveX;
      var delBtnWidth = _this.data.delBtnWidth;
      var txtStyle = "";
      if (disX == 0 || disX < 0) {//如果移动距离小于等于0，文本层位置不变
        txtStyle = "left:0px";
      } else if (disX > 0) {//移动距离大于0，文本层left值等于手指移动距离
        txtStyle = "left:-" + disX + "px";
        test = false;
        if (disX >= delBtnWidth) {
          //控制手指移动距离最大值为删除按钮的宽度
          txtStyle = "left:-" + delBtnWidth + "rpx";
        }
      }
      //获取手指触摸的是哪一项
      var index = e.currentTarget.dataset.index,
        shopCarList = _this.data.shopCarList;
        shopCarList[index].txtStyle = txtStyle;
      _this.setData({
        shopCarList: shopCarList,
        scrollSwitch: test
      })
    }
  },
  touchE: function (e) {
    if (e.changedTouches.length == 1) {
      //手指移动结束后水平位置
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离
      var disX = _this.data.startX - endX;
      var delBtnWidth = _this.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮
      var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "rpx" : "left:0px";
      //获取手指触摸的是哪一项
      var index = e.currentTarget.dataset.index,
        shopCarList = _this.data.shopCarList;
        shopCarList[index].txtStyle = txtStyle;
      _this.setData({
        shopCarList: shopCarList
      })
      setTimeout(function () {
        _this.setData({
          scrollSwitch: true,
        })
      }, 200)
    }
  },
  //获取元素自适应后的实际宽度
  getEleWidth: function (w) {
    var real = 0;
    try {
      var res = wx.getSystemInfoSync().windowWidth;
      var scale = (750 / 2) / (w / 2);//以宽度750px设计稿做宽度的自适应
      // console.log(scale);
      real = Math.floor(res / scale);
      return real;
    } catch (e) {
      return false;
      // Do something when catch error
    }
  },
  initEleWidth: function () {
    var delBtnWidth = this.getEleWidth(_this.data.delBtnWidth);
    _this.setData({
      delBtnWidth: delBtnWidth
    });
  },
  // 去**页
  goNavTo: function (e) {
    app.goNavTo(e, _this.data.buttonClicked, _this);
  },
  // 返回上一页
  goBack: function (e) {
    app.goBack(e, _this.data.buttonClicked, _this);
  },
})